package com.threeSales.dao;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.threeSales.entity.User;
import com.threeSales.entity.UserNexus;

public class TestDao {
	

	@Test
	public void testAddUser(){
		User user = new User();
		UserDao userdao = new UserDao();
		user.setId("brre");
		user.setName(12221);
		user.setPasswords("rrr");
		user.setFid("444");
		
		userdao.addUser(user);
	}
	
	
/*	@Test
	public void testDateSource(){
		ApplicationContext applicationContext= null;
		JdbcTemplate jdbcTemplate = null;
		
		applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
		DataSource dataSource = applicationContext.getBean(DataSource.class);
		try {
			System.out.println(dataSource.getConnection());
		} catch (SQLException e) {
			throw  new RuntimeException(e);
		}
	}*/
	
	
	
	@Test
	public void listUserNexusFindbyfid(){
		ApplicationContext applicationContext= null;
		JdbcTemplate jdbcTemplate = null;
		
		applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate");
		DataSource dataSource = applicationContext.getBean(DataSource.class);
		String name = "1";
		String pwd ="1";
		String sql1 = "select * from tb_user where name=? and passwords=?";
		User user = jdbcTemplate.queryForObject(sql1, new Object[] {name, pwd }, new BeanPropertyRowMapper(User.class));
		System.out.println(user);
		String fid="4534b1fdad034035b46de69d5fbdb20a";
		//int id = 23;
		String sql = "SELECT * FROM tb_user_nexus WHERE fid=?";
		List list = new ArrayList();
		System.out.println(jdbcTemplate.queryForList(sql,new Object[]{fid}));
		list = jdbcTemplate.queryForList(sql,new Object[]{fid});
		Iterator it = list.iterator(); 
		HashMap userMap = new HashMap();
		List<UserNexus> c = new ArrayList<UserNexus>();
	    while(it.hasNext()) {  
	    	UserNexus userNexus = new UserNexus();
	        userMap = (HashMap) it.next(); 
	      /*  System.out.println(userMap);
	        System.out.print(userMap.get("id") + "\t");  
	        System.out.print(userMap.get("fid") + "\t");  
	        System.out.print(userMap.get("lowerid") + "\t");  
	        System.out.println(userMap.get("level") + "\t"); */
	        userNexus.setId((int)userMap.get("id"));
	        userNexus.setFid((String)userMap.get("fid"));
	        userNexus.setLowerid((String)userMap.get("lowerid"));
	        userNexus.setLevel((String)userMap.get("level"));
	        c.add(userNexus);
	    }  
/*	    System.out.println("=="+c.getClass());
	    System.out.println("------------------------");
	    System.out.println(c.size());*/
	    System.out.println(c.get(0).getLowerid()+c.get(0).getLevel());
	    System.out.println("id="+c.get(1).getLowerid()+"级别："+c.get(1).getLevel());
	    System.out.println("id="+c.get(2).getLowerid()+"级别："+c.get(2).getLevel());
	    System.out.println("id="+c.get(3).getLowerid()+"级别："+c.get(3).getLevel());
	    
	    


		
		
	}
}
